﻿using Microsoft.AspNetCore.Http;
using System;
using System.Net.Http;
using System.Text;


namespace Microsoft.AspNetCore.Http
{
    /// <summary>
    /// httprequest的扩展方法
    /// </summary>
    public static class HttpRequestHelper
    {
        private static HttpClient client;

        static HttpRequestHelper()
        {
            if (client == null)
            {
                client = new HttpClient();
                client.DefaultRequestHeaders.Accept.TryParseAdd("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
                client.DefaultRequestHeaders.AcceptEncoding.TryParseAdd("gzip, deflate, br");
                client.DefaultRequestHeaders.UserAgent.TryParseAdd("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
            }
        }

        /// <summary>
        /// 获取请求的绝对路径 url
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetAbsoluteUri(this HttpRequest request)
        {
            return new StringBuilder()
                .Append(request.Scheme)
                .Append("://")
                .Append(request.Host.Value)
                .Append(request.PathBase.Value)
                .Append(request.Path.Value)
                .Append(request.QueryString.Value)
                .ToString();
        }

        /// <summary>
        /// 获取当前访问的域名, 包含/后面的内容, 但不包含查询字符串
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetCurrentRequestPath(this HttpRequest request)
        {
            return new StringBuilder()
                .Append(request.Scheme)
                .Append("://")
                .Append(request.Host.Value)
                .Append(request.PathBase.Value)
                .Append(request.Path.Value)
                .ToString();
        }

        /// <summary>
        /// 获取当前网站的域名
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetCurrentDomain(this HttpRequest request)
        {
            return new StringBuilder()
                .Append(request.Scheme)
                .Append("://")
                .Append(request.Host.Value)
                .ToString();
        }

        /// <summary>
        /// 请求http地址, 获取字符串响应内容
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetHttpStringResponse(string url)
        {
            var html = string.Empty;
            try
            {
                html = client.GetStringAsync(url).GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                var err = ex;
                while (err.InnerException != null)
                {
                    err = err.InnerException;
                }
                Console.WriteLine($"http请求出现异常,url:{url}\r\n{err.Message}\r\n{err.StackTrace}");
            }
            return html;
        }
    }
}
